﻿using System;
using System.Collections.Generic;
using System.IO;
//using DatabaseHandler;
using Emgu.CV;
using Emgu.CV.Features2D;
using Emgu.CV.Structure;

namespace ImageHandler
{
	public class ImageMatch
	{

		#region fields
		private SURFDetector _surfaceDetector;
		#endregion

		public ImageMatch()
		{
			_surfaceDetector = new SURFDetector(500, true);
		}

		public ImageMatch(ImageMatch toCopy)
		{
			TotalPairMatches = toCopy.TotalPairMatches;
			TotalPossiblePairMatches = toCopy.TotalPossiblePairMatches;
			PctOfMatch = toCopy.PctOfMatch;
			ImageFileName = toCopy.ImageFileName;
			SearchTime = toCopy.SearchTime;
			GivenImage = toCopy.GivenImage;
			GivenImageFeatures = toCopy.GivenImageFeatures;
			TemplateImage = toCopy.TemplateImage;
			TemplateImageFeatures = toCopy.TemplateImageFeatures;
			MatchedPairs = toCopy.MatchedPairs;
			//MatchedDBStamp = toCopy.MatchedDBStamp;
		  MatchedDBStampId = toCopy.MatchedDBStampId;
		}

		public int TotalPairMatches { get; set; }
		public int TotalPossiblePairMatches { get; set; }
		public float PctOfMatch { get; set; }
		public String ImageFileName { get; set; }
		public float SearchTime { get; set; }
		public Image<Gray, byte> GivenImage { get; private set; }
		public ImageFeature[] GivenImageFeatures { get; set; }
		public Image<Gray, byte> TemplateImage { get; private set; }
		public ImageFeature[] TemplateImageFeatures { get; set; }
		public List<KeyValuePair<int, int>> MatchedPairs { get; set; }
		//public Stamp MatchedDBStamp { get; set; }
    public Guid MatchedDBStampId { get; set; }

		private void CalculateGivenImageFeatures()
		{
			if (GivenImage != null)
			{
				GivenImageFeatures = _surfaceDetector.DetectFeatures(GivenImage, null);				
			}
		}
		private void CalculateTemplateImageFeatures()
		{
			if (TemplateImage != null)
			{
				TemplateImageFeatures = _surfaceDetector.DetectFeatures(TemplateImage, null);
			}
		}

		/// <summary>
		/// LoadGivenImageFromImageFile loads GivenImage and GivenImageFeatures from
		/// the filename.	
		/// </summary>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public bool LoadGivenImageFromImageFile(String fileName)
		{
			if (!File.Exists(fileName))
				return false;

			GivenImage = new Image<Gray, byte>(fileName);
			CalculateGivenImageFeatures();


			return true;
		}

		public bool LoadGivenImageFromFeatureFile(String fileName)
		{

            if (!File.Exists(fileName))
                return false;

		    ImageFeature[] features;
		    FileImagePersistence.LoadImageFeatures(out features, fileName);

		    GivenImageFeatures = features;

			return true;
		}

		public bool LoadGivenImageFromStream()
		{
			return true;
		}

		public bool LoadTemplateImageFromImageFile(String fileName, bool generateFeatures = true)
		{

			if (!File.Exists(fileName))
				return false;

			TemplateImage = new Image<Gray, byte>(fileName);
			if (generateFeatures)
				CalculateTemplateImageFeatures();

			return true;
		}

		public bool LoadTemplateImageFromFeatureFile(String fileName)
		{
            if (!File.Exists(fileName))
                return false;

            ImageFeature[] features;
            FileImagePersistence.LoadImageFeatures(out features, fileName);
		    TemplateImageFeatures = features;
            
			return true;
		}

		public bool LoadTemplateImageFromStream()
		{
			return true;
		}
	}

}
